package com.android.pI;

import com.android.pI.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private boolean timerHasStarted = false;
	public Workout wk;
	public TextView textTimer;
	public TextView TextEX;
	public TextView Best;
	public long startTime = 0;
	public long interval = 1000;
	private Bundle bundle;
	private Button start;
	private Button save;
	private Button pause;
	private Button next;
	public RMTimer cdt;
	public int count = 0;
	
	// Smell:
	// Bad code smell: The Object Orientation Abusers (Temporary Field) & The Dispensables (Dead Code)
	// The MainActivity Class is very large and and some dead variables not being used.
	// Refactoring:
	// We removed the dead variables. We also changed variables to the appropriate 
	// type and moved some from the MainActivity Class to the RMTImer Class
	
	/*
	Smelly Code
	
	public long timeElapsed;
	public boolean timerHasStarted = false;
	public boolean onPause = false;
	public int count = 0;
	public Workout wk;
	public TextView textTimer;
	public String lastEx;
	public int pr;
	public int exId;
	public TextView TextEX;
	public TextView Best;
	public TextView timeElapsedView;
	public String exercise;
	public long lastUpdate = 0;
	public long startTime = 0;
	public long interval = 1000;
	public long newStartTime = 0;
	private Bundle bundle;
	Button start;
	Button save;
	Button pause;
	Button next;
	ImageView image;
	public RMTimer cdt;
	DataBaseHelper dbh;
	
	
	New Code
	
	private boolean timerHasStarted = false;
	public Workout wk;
	public TextView textTimer;
	public TextView TextEX;
	public TextView Best;
	public long startTime = 0;
	public long interval = 1000;
	private Bundle bundle;
	private Button start;
	private Button save;
	private Button pause;
	private Button next;
	public RMTimer cdt;
	public int count = 0;
	
	Moved to different RMTimer Class
		
		private int exId;
		private String lastEx;
		private long lastUpdate = 0;
		private int bestPerfomance;
	
*/	
	/** Called when the activity is first created. */
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timer);
		getWorkoutName();
		DataBaseHelper dbh = new DataBaseHelper(this);
		Cursor cursor = dbh.fetchExs(bundle.getString("workout"));

		start = (Button) findViewById(R.id.start);
		start.setOnClickListener(mStartListener);
		pause = (Button) findViewById(R.id.pause);
		pause.setOnClickListener(mPauseListener);
		save = (Button) findViewById(R.id.save1);
		save.setOnClickListener(mSaveListener);
		next = (Button) findViewById(R.id.next);
		next.setOnClickListener(mNextListener);
		textTimer = (TextView) this.findViewById(R.id.timer);
		TextEX = (TextView) this.findViewById(R.id.TextEx);
		Best = (TextView) this.findViewById(R.id.best);
		textTimer.setText(textTimer.getText());
		wk = new Workout(cursor);
		cdt = new RMTimer(wk.getTime(0), interval, wk.getName(0), wk.getEID(0),wk.getBest(0));
	}
	
    private void createNote(int currentEx, String currentEname) {
    	int eID = currentEx;
    	String ex = currentEname;
    	Intent intent = new Intent();// intent is an envelop
		// arg1 is the current class and arg2 is the class to pass to
		intent.setClass(MainActivity.this,LogEditActivity.class);					
		// bundle is the letter
		Bundle bundle = new Bundle(); 
		// arg1 is the keyword for the string , arg2 is the sting to pass
		bundle.putInt("e_id", eID);
		bundle.putString("ex_name", ex);
		bundle.putInt("case", 2);
		intent.putExtras(bundle);
		startActivity(intent);
    }
    
	private void getWorkoutName() {
		if (MainActivity.this.getIntent().getExtras() != null) {
			bundle = this.getIntent().getExtras();// get the intent & bundle
													// passed by X
			Toast.makeText(MainActivity.this, bundle.getString("workout"),
					Toast.LENGTH_SHORT).show();
		}
	}

	View.OnClickListener mStartListener = new OnClickListener() {
		public void onClick(View v) {
			if (!timerHasStarted) {
				cdt.start();
				timerHasStarted = true;
			}
		}
	};
	
	View.OnClickListener mSaveListener = new OnClickListener() {
		public void onClick(View v) {
			createNote(cdt.GetExId(), cdt.GetEx());
			cdt.cancel();
			timerHasStarted = false;
		}
	};
	
	View.OnClickListener mPauseListener = new OnClickListener() {
		public void onClick(View v) {
			cdt.cancel();
			cdt.pause();
			timerHasStarted = false;
		}
	};

	View.OnClickListener mNextListener = new OnClickListener() {
		public void onClick(View v) {
			cdt.next();
			timerHasStarted = false;
		}
	};

	// RMTimer class
	public class RMTimer extends CountDownTimer {
		
		private int exId;
		private String lastEx;
		private long lastUpdate = 0;
		private int bestPerfomance;
	
		
		public RMTimer(long startTime, long interval, String exercise, int eID, int b) {
			super(startTime, interval);
			TimeFormater timeFormater = new TimeFormater (startTime);
			textTimer.setText("Time remaining:  " + timeFormater.getTime());
			TextEX.setText("Exercise:  " + exercise);
			Best.setText("Your Best:  " + b);
			setEx(exercise);
			setExId(eID);
			setPr(b);
			SetLastUpdate(startTime);
		}

		public String GetEx() {
			return lastEx;
		}

		public void setEx(String ex) {
			lastEx = ex;
		}
		public int GetPr() {
			return bestPerfomance;
		}

		public void setPr(int b) {
			bestPerfomance = b;
		}
		public int GetExId() {
			return exId;
		}

		public void setExId(int ei4) {
			exId = ei4;
		}
		public void onFinish() {
	
		if (count == wk.size()-1) {
			textTimer.setText("Time remaining:  00:00");
			Toast.makeText(MainActivity.this, "You are done with this " +
					"workout!!",Toast.LENGTH_LONG).show();
			textTimer.setText("You are done with" + lastEx + "!");
			;
		} else{
			textTimer.setText("Time remaining:  00:00");
			Toast.makeText(MainActivity.this, "You are done with " + lastEx + 
					"!",Toast.LENGTH_SHORT).show();}
	}
		

		public void pause() {
			int pRecord1 = cdt.GetPr();
			String e = cdt.GetEx();
			long d = cdt.GetLastUpdate();
			int ei5 = cdt.GetExId();
			cdt = new RMTimer(d, interval, e, ei5, pRecord1);
		}

		public void next() {
			cdt.cancel();
			if (count < (wk.size() - 1)) {
				count++;
				cdt = new RMTimer(wk.getTime(count), interval,
						wk.getName(count), wk.getEID(count), wk.getBest(count));
			} else {
				count = 0;
				cdt = new RMTimer(wk.getTime(0), interval,
						wk.getName(0), wk.getEID(0), wk.getBest(count));
			}
		}
		
		// Smell:
		// Bad code smell: The Bloaters (Long Method)
		// The getTime method is long and is in a already long class.
		// Refactoring:
		// We will remove the getTime method and create a new TimeFormater class
		
/*		
 		Smelly Code
 		
		@Override
		public void onTick(long millisUntilFinished) {
			textTimer.setText("Time remaining:  "
					+ getTime(millisUntilFinished));
			SetLastUpdate(millisUntilFinished);
		}
	
		public String getTime(long millis) {
			int seconds = (int) ((millis / 1000) % 60);
			int minutes = (int) ((millis / 1000) / 60);
			String time = String.format("%02d:%02d", minutes, seconds);
			return time;
		}
	
		New Code
		
		public String getTime(long millis) {
			int seconds = (int) ((millis / 1000) % 60);
			int minutes = (int) ((millis / 1000) / 60);
			String time = String.format("%02d:%02d", minutes, seconds);
			return time;
		}
		
		New Class TimeFormater
	
		public class TimeFormater {

		public int seconds;
		public String time;
		public int minutes;

		// The constructor method
		public TimeFormater	(long millis) {
			int seconds = (int) ((millis / 1000) % 60);
			int minutes = (int) ((millis / 1000) / 60);
			time = String.format("%02d:%02d", minutes, seconds);
		}
	
		public String getTime(){
			return time;
		}
	}
		
	*/
		
		
		@Override
		public void onTick(long millisUntilFinished) {
			TimeFormater timeFormater = new TimeFormater (millisUntilFinished);
			textTimer.setText("Time remaining:  "
					+ timeFormater.getTime());
			SetLastUpdate(millisUntilFinished);
		}
		
		
		public void SetLastUpdate(long millisUntilFinished) {
			lastUpdate = millisUntilFinished;
		}

		public long GetLastUpdate() {
			return lastUpdate;
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

}